﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;

namespace JM.JCY.SharePointComponent.Announcement.CreateAnnounce
{
    public partial class CreateAnnounceUserControl : UserControl
    {
        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }
        public string _userName
        {
            set { ViewState["_userName"] = value; }
            get { return (string)ViewState["_userName"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ApplicationContext.Current.GetKeyValue("userId") != null)
                {
                    _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
                    _userName = ApplicationContext.Current.UserName;
                }
                using (UserManagementProxy userProxy = new UserManagementProxy())
                {
                    DataSetUserManagement.T_UserInfoDataTable userDT = userProxy.GetUser(0, string.Empty);
                    if (userDT.Rows.Count > 0)
                    {
                        box1View.DataSource = userDT;
                        box1View.DataTextField = "UserName";
                        box1View.DataValueField = "ID";
                        box1View.DataBind();
                    }
                }

            }
        }

        protected void imgSaveDraft_Click(object sender, ImageClickEventArgs e)
        {
            SaveAnnounce(JCYConstant.Status.Draft);
            Response.Redirect("ManageAnnounce.aspx");
        }

        protected void imgSavePublish_Click(object sender, ImageClickEventArgs e)
        {
            SaveAnnounce(JCYConstant.Status.Publish);
            Response.Redirect("ManageAnnounce.aspx");
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ManageAnnounce.aspx");
        }

        private void SaveAnnounce(string status)
        {
            using (AnnounceProxy announccProxy = new AnnounceProxy())
            {
                // update announce table
                AnnounceDataSet.T_AnnounceDataTable annouceDT = new AnnounceDataSet.T_AnnounceDataTable();
                AnnounceDataSet.T_Announce_ReceiverDataTable annouceReceiverDT = new AnnounceDataSet.T_Announce_ReceiverDataTable();
                AnnounceDataSet.T_AnnounceRow newAnnounce = annouceDT.NewT_AnnounceRow();
                newAnnounce.Subject = this.txtSubject.Text;
                newAnnounce.Summary = this.txtSummary.Text;
                newAnnounce.CreatorUserId = _userId;
                newAnnounce.Status = status;

                // add by Hong Liang 2012-05-02
                if (this.filePicture.HasFile)
                {
                    newAnnounce.AttachmentUrl = JCYSharePointHelper.UploadFileToAttachment(this.filePicture, Guid.NewGuid().ToString(), JCYConstant.SharePointListName.Attachment);
                    newAnnounce.AttachmentName = this.filePicture.FileName;
                }
                JCY.Common.JCYUtility.UpdateCommonFields(newAnnounce, "aa");
                annouceDT.AddT_AnnounceRow(newAnnounce);
                announccProxy.UpdateAnnouceSingle(annouceDT);

                // update announce receiver table
                string s = Request.Form[box2View.UniqueID];

                foreach (var item in s.Split(','))
                {
                    using (UserManagementProxy userProxy = new UserManagementProxy())
                    {
                        var newReceiver = annouceReceiverDT.NewT_Announce_ReceiverRow();
                        newReceiver.AnnounceId = annouceDT[0].ID;
                        newReceiver.ReceiverUserId = Int32.Parse(item);
                        newReceiver.ReceiverUserName = userProxy.GetUser(Int32.Parse(item), string.Empty)[0].UserName;
                        newReceiver.Status = JCY.Common.JCYConstant.ReadStatus.UnRead;
                        JCY.Common.JCYUtility.UpdateCommonFields(newReceiver, "aa");
                        annouceReceiverDT.AddT_Announce_ReceiverRow(newReceiver);
                    }

                    announccProxy.UpdateAnnounceReceiverSingle(annouceReceiverDT);
                }

            }
        }

    }
}
